Basic Practice Problems in C++
Posted on December 18, 2023 by Vishesh Namdev
Python
C
C++
Java
Basic Practice Problems based on chapter which you learn in previous Tutorials.
1. Objects and Classes
#include <iostream>
#include <string>
class Book {
public:
std::string title;
std::string author;
int publicationYear;
};
int main() {
Book book1;
book1.title = "The Catcher in the Rye";
book1.author = "J.D. Salinger";
book1.publicationYear = 1951;
Book book2;
book2.title = "To Kill a Mockingbird";
book2.author = "Harper Lee";
book2.publicationYear = 1960;
std::cout << "Book 1: " << book1.title << " by " << book1.author << " (" << book1.publicationYear << ")\n";
std::cout << "Book 2: " << book2.title << " by " << book2.author << " (" << book2.publicationYear << ")\n";
return 0;
}
#include <string>
class Book {
public:
std::string title;
std::string author;
int publicationYear;
};
int main() {
Book book1;
book1.title = "The Catcher in the Rye";
book1.author = "J.D. Salinger";
book1.publicationYear = 1951;
Book book2;
book2.title = "To Kill a Mockingbird";
book2.author = "Harper Lee";
book2.publicationYear = 1960;
std::cout << "Book 1: " << book1.title << " by " << book1.author << " (" << book1.publicationYear << ")\n";
std::cout << "Book 2: " << book2.title << " by " << book2.author << " (" << book2.publicationYear << ")\n";
return 0;
}
2. Find Area using Inheritance Classes
#include <iostream>
class Shape {
public:
virtual double calculateArea() const = 0;
virtual double calculatePerimeter() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() const override {
return 3.14 * radius * radius;
}
double calculatePerimeter() const override {
return 2 * 3.14 * radius;
}
; };
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double calculateArea() const override {
return length * width;
}
double calculatePerimeter() const override {
return 2 * (length + width);
}
; };
int main() {
Circle circle(5.0);
Rectangle rectangle(4.0, 6.0);
std::cout << "Circle - Area: " << circle.calculateArea() << ", Perimeter: " << circle.calculatePerimeter() << std::endl;
std::cout << "Rectangle - Area: " << rectangle.calculateArea() << ", Perimeter: " << rectangle.calculatePerimeter() << std::endl;
return 0;
}
class Shape {
public:
virtual double calculateArea() const = 0;
virtual double calculatePerimeter() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() const override {
return 3.14 * radius * radius;
}
double calculatePerimeter() const override {
return 2 * 3.14 * radius;
}
; };
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double calculateArea() const override {
return length * width;
}
double calculatePerimeter() const override {
return 2 * (length + width);
}
; };
int main() {
Circle circle(5.0);
Rectangle rectangle(4.0, 6.0);
std::cout << "Circle - Area: " << circle.calculateArea() << ", Perimeter: " << circle.calculatePerimeter() << std::endl;
std::cout << "Rectangle - Area: " << rectangle.calculateArea() << ", Perimeter: " << rectangle.calculatePerimeter() << std::endl;
return 0;
}